﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.Controls.GridEx
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.RobotJog.Controls;

public static class GridEx
{
  public static bool SwapRows(this Grid grid, int row1, int row2)
  {
    UIElement element = (UIElement) null;
    UIElement uiElement = (UIElement) null;
    for (int row = 0; row < grid.RowDefinitions.Count; ++row)
    {
      element = grid.GetChild(row, 0, typeof (UIElement));
      UIElement child = grid.GetChild(row, 1, typeof (UIElement));
      if (element != null && child != null)
      {
        if (element is Border border1)
          border1.Background = (Brush) null;
        if (child is Border border2)
          border2.Background = (Brush) null;
      }
    }
    if (row1 < 0 || row1 >= grid.RowDefinitions.Count || row2 < 0 || row2 >= grid.RowDefinitions.Count || row1 == row2)
      return false;
    for (int col = 0; col < grid.ColumnDefinitions.Count; ++col)
    {
      element = grid.GetChild(row1, col, typeof (UIElement));
      UIElement child = grid.GetChild(row2, col, typeof (UIElement));
      uiElement = grid.GetChild(row2, 0, typeof (UIElement));
      Grid.SetRow(element, row2);
      Grid.SetRow(child, row1);
    }
    RowDefinition rowDefinition1 = grid.RowDefinitions[row1];
    RowDefinition rowDefinition2 = grid.RowDefinitions[row2];
    grid.RowDefinitions.Remove(rowDefinition1);
    grid.RowDefinitions.Remove(rowDefinition2);
    if (row1 > row2)
    {
      grid.RowDefinitions.Insert(row2, rowDefinition1);
      grid.RowDefinitions.Insert(row1, rowDefinition2);
    }
    else
    {
      grid.RowDefinitions.Insert(row1, rowDefinition2);
      grid.RowDefinitions.Insert(row2, rowDefinition1);
    }
    if (element is Border border3)
      border3.Background = TxStatusColor.ToBrush(TxStatusColor.Background("Emphasize1"));
    if (uiElement is Border border4)
      border4.Background = TxStatusColor.ToBrush(TxStatusColor.Background("Emphasize1"));
    return true;
  }

  public static bool SwapColumns(this Grid grid, int col1, int col2)
  {
    if (col1 < 0 || col1 >= grid.ColumnDefinitions.Count || col2 < 0 || col2 >= grid.ColumnDefinitions.Count || col1 == col2)
      return false;
    for (int row = 0; row < grid.RowDefinitions.Count; ++row)
    {
      UIElement child1 = grid.GetChild(row, col1, typeof (UIElement));
      UIElement child2 = grid.GetChild(row, col2, typeof (UIElement));
      if (child1 != null)
        Grid.SetColumn(child1, col2);
      if (child2 != null)
        Grid.SetColumn(child2, col1);
    }
    ColumnDefinition columnDefinition1 = grid.ColumnDefinitions[col1];
    ColumnDefinition columnDefinition2 = grid.ColumnDefinitions[col2];
    grid.ColumnDefinitions.Remove(columnDefinition1);
    grid.ColumnDefinitions.Remove(columnDefinition2);
    if (col1 >= grid.ColumnDefinitions.Count)
      grid.ColumnDefinitions.Add(columnDefinition2);
    else
      grid.ColumnDefinitions.Insert(col1, columnDefinition2);
    if (col2 >= grid.ColumnDefinitions.Count)
      grid.ColumnDefinitions.Add(columnDefinition1);
    else
      grid.ColumnDefinitions.Insert(col2, columnDefinition1);
    return true;
  }

  public static void ReorderColumns(this Grid grid, List<string> listOrder)
  {
    int num = 0;
    foreach (string str in listOrder)
    {
      foreach (ColumnDefinition columnDefinition in (IEnumerable<ColumnDefinition>) grid.ColumnDefinitions)
      {
        if (str != "" && str == columnDefinition.Name)
        {
          int col2 = columnDefinition.Index();
          if (grid.ColumnDefinitions[num].IsSplitter())
            ++num;
          if (num != col2)
          {
            grid.SwapColumns(num, col2);
            break;
          }
          break;
        }
      }
      ++num;
    }
  }

  public static UIElement GetChild(this Grid grid, int row, int col, Type targetType)
  {
    UIElement child1 = (UIElement) null;
    foreach (UIElement child2 in grid.Children)
    {
      int row1 = Grid.GetRow(child2);
      int column = Grid.GetColumn(child2);
      if (row1 == row && column == col)
      {
        if (child2 is Border border)
        {
          if (border.Child != null && border.Child.GetType() == targetType)
          {
            child1 = border.Child;
            break;
          }
          child1 = (UIElement) border;
        }
        else if (child2.GetType() == targetType)
        {
          child1 = child2;
          break;
        }
      }
    }
    return child1;
  }
}
